<?php

class Votosmsinstituto extends Controller {

    public function __construct() {
        parent::__construct();
        Session::init();
        $logged = Session::get('loggedIn');
        $role = Session::get('role');

        if ($logged == false || $role != 'owner') {
            Session::destroy();
            header('location: ' . URL . 'login');
            exit;
        }
        $this->view->js = array('votosmsinstituto/js/default.js');
    }

    public function index() {
        $this->view->singleList = $this->model->institutoList();
        $this->view->formAction = URL . 'votosmsinstituto/searchPatrullero/';
        $this->view->display = 'inline';
        $this->view->totalVotos = $this->model->totalVotos();
        $this->view->render('votosmsinstituto/index');
    }

    public function municipio($id) {
        print json_encode($this->model->municipioList($id));
    }

    public function parroquia($id) {
        $this->view->singleList = $this->model->parroquiaList($id);
        $this->view->render('votosmsinstituto/parroquia', true);
    }

    public function centro($id) {
        $this->view->singleList = $this->model->centroList($id);
        $this->view->render('votosmsinstituto/centro', true);
    }

    public function patrulla($id) {
        print json_encode($this->model->patrullaList($id));
    }

    public function patrullero($id) {
        print json_encode($this->model->patrulleroShow($id));
    }

    public function searchPatrullero() {
        print json_encode($this->model->patrulleroShow($_POST['cedula']));
    }

    public function reporteCentroSave($id) {
        $data = $_POST;
        $this->model->reporteCentroSave($data, $id);
    }

    public function reporte1() {
        $this->view->singleList = $this->model->municipioList();
//        $this->view->parroquia = $this->model->parroquiaList($id);
        $this->view->formAction = URL . 'votosmsinstituto/create/';
        $this->view->display = 'none';
        $this->view->render('votosmsinstituto/reporte1');
    }

    public function grafica1() {
        $this->view->singleList = $this->model->municipioList();
        $this->view->formAction = URL . 'votosmsinstituto/create/';
        $this->view->display = 'none';
        $this->view->render('votosmsinstituto/grafica1');
    }

    public function create() {
        $data = $_POST;
        // @TODO: Do your error checking!

        $this->model->create($data);
        // header('location: ' . URL . 'user');
    }

    public function edit($id) {
        $this->view->carga = $this->model->singleSearch($id);
        $this->view->formAction = URL . 'carga/editSave/' . $id;
        $this->view->display = 'block';
        $this->view->render('votosmsinstituto/carga_form', true);
    }

    public function editSave($id) {
        $data = $_POST;
        $this->model->editSave($data, $id); //
    }

    public function disable($id) {
        $this->model->disable($id);
    }

    public function enable($id) {
        $this->model->enable($id);
    }

    public function massive() {
        $this->view->formAction = URL . 'votosmsinstituto/massiveUpload/';
        $this->view->display = 'block';
        $this->view->render('votosmsinstituto/massive');
    }

    public function massiveUpload() {
        $remplace = array('([^,0-9])');
        $listPhone = preg_replace($remplace, '', $_POST['cedulas']);
        $phone = explode(',', $listPhone);
        foreach ($phone as $key2 => $value2) {
            $data['voto'] = 1;
            $this->model->massiveUpload($data, $value2);
            //mysql_query("UPDATE `eleccionesccc2012`.`cne` SET `voto` = '1' WHERE `cne`.`cedula` = " . $value2);
        }
        $message['messageAction'] = 'Se han cargado';
        print json_encode($message);
    }

    public function sendpdf() {
        //setcookie("tempPDF", $_POST['data']);
        Session::set('tempPDF', $_POST['data']);
    }

    public function printpdf() {
        //$this->view->post = $_COOKIE['tempPDF'];
        $this->view->post = $_SESSION['tempPDF'];
        $data['title_reporte'] = '';
        $data['location'] = 'votosmsinstituto/pdf';
        $data['autor'] = 'CCC Yaracuy';
        $data['title'] = 'Reporte Estadal ';
        $data['subject'] = 'Comando de Campana Yaracuy 2012';
        $data['keywords'] = 'reporte, 1x10';
        $data['output'] = 'reporte.pdf';
        $this->view->genPDF($data, 'L');
    }

    public function pdf($reporte, $id = null) {
        if ($reporte == 'index') {
            $this->view->singleList = $this->model->municipioList();
            $data['title_reporte'] = '<h1 class="h1">Estadisticas SMS Municipios</h1>';
        } elseif ($reporte == 'parroquia') {
            $this->view->singleList = $this->model->parroquiaList($id);
            $data['title_reporte'] = '<h1 class="h1">Estadisticas SMS Parroquias</h1>';
        } elseif ($reporte == 'centro') {
            $this->view->singleList = $this->model->centroList($id);
            $data['title_reporte'] = '<h1 class="h1">Estadisticas SMS Centro de Votaciones</h1>';
        } elseif ($reporte == 'patrulla') {
            $this->view->singleList = $this->model->patrullaList($id);
            $data['title_reporte'] = '<h1 class="h1">Estadisticas SMS Jefe de 1x10</h1> ';
        } elseif ($reporte == 'patrullero') {
            $this->view->singleList = $this->model->patrulleroShow($id);
            $data['title_reporte'] = '<h1 class="h1">Estadisticas SMS Patrulleros</h1> ';
        }
        //$this->view->singleList = $this->model->municipioList();
        //$this->view->formAction = URL . 'votosmsinstituto/pdf/';
        $this->view->display = 'none';
        $data['location'] = 'votosmsinstituto/pdf/' . $reporte;
        $data['autor'] = 'CCC Yaracuy';
        $data['title'] = 'Reporte Estadal ';
        $data['subject'] = 'Comando de Campana Yaracuy 2012';
        $data['keywords'] = 'reporte, 1x10';
        $data['output'] = 'reporte.pdf';
        $this->view->genPDF($data, 'L');
    }

}